package com.tencent.qf.common.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tencent.qf.common.exception.ErrorCode;
import com.tencent.qf.common.exception.ServiceException;
import lombok.Data;
import lombok.experimental.Accessors;
import org.slf4j.MDC;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.Objects;

import static com.tencent.qf.common.constants.WebConstants.TRACE_ID;

@Data
@Accessors(chain = true)
public class ActivityPlatformHttpResult<T> implements Serializable {

    /**
     * 错误码
     *
     * @see ErrorCode#getCode()
     */
    private String code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 错误提示，用户可阅读
     *
     * @see ErrorCode#getMsg() ()
     */
    private String msg;

    private String traceId;


    public static <T> ActivityPlatformHttpResult<T> error(String code, String message) {
        ActivityPlatformHttpResult<T> result = new ActivityPlatformHttpResult<>();
        result.code = code;
        result.msg = message;
        result.setTraceId(MDC.get(TRACE_ID));
        return result;
    }

    public static <T> ActivityPlatformHttpResult<T> success(T data) {
        ActivityPlatformHttpResult<T> result = new ActivityPlatformHttpResult<>();
        result.code = String.valueOf(ErrorCode.SUCCESS.getCode());
        result.msg = ErrorCode.SUCCESS.getMsg();
        result.data = data;
        result.setTraceId(MDC.get(TRACE_ID));
        return result;
    }

    public static boolean isSuccess(String code) {
        return Objects.equals(code, ErrorCode.SUCCESS.getCode());
    }

    @JsonIgnore // 避免 jackson 序列化
    public boolean isSuccess() {
        return isSuccess(code);
    }

    @JsonIgnore // 避免 jackson 序列化
    public boolean isError() {
        return !isSuccess();
    }

}
